import { useProject } from '../stores/modules/project.js'
import { useUser } from '../stores/modules/user.js'
import layout from '../layout/IndexView.vue'
import NProgress from './NProgress.js'

const authRouteAdmin = [
  {
    path: '/about',
    name: 'about',
    component: layout,
    redirect: '/about/about1',
    meta: {
      title: '多级菜单',
      icon: 'el-icon-menu'
    },
    children: [
      {
        path: '/about/about1',
        name: 'about1',
        component: () => import('@/views/about/AboutOne.vue'),
        meta: {
          title: 'about1'
        }
      },
      {
        path: '/about/about2',
        name: 'about2',
        component: () => import('@/views/about/AboutTwo.vue'),
        redirect: '/about/about2/about2-1',
        meta: {
          title: 'about2'
        },
        children: [
          {
            path: '/about/about2/about2-1',
            name: 'about2-1',
            component: () => import('@/views/about/AboutTwoOne.vue'),
            meta: {
              title: 'about2-1'
            }
          },
          {
            path: '/about/about2/about2-2',
            name: 'about2-2',
            component: () => import('@/views/about/AboutTwoTwo.vue'),
            meta: {
              title: 'about2-2'
            }
          }
        ]
      }
    ]
  },
  {
    path: '/echarts',
    name: 'echarts',
    component: layout,
    redirect: '/echarts/index',
    meta: {
      title: '图表',
      icon: 'el-icon-s-data'
    },
    children: [
      {
        path: '/echarts/index',
        name: 'echartsIndex',
        component: () => import('@/views/echarts/EIndex.vue'),
        meta: {
          title: '图表'
        }
      },
    ]
  },
  {
    path: '/cc',
    name: 'CommonComponents',
    component: layout,
    redirect: '/cc/cform',
    meta: {
      title: '公用组件',
      icon: 'el-icon-s-grid'
    },
    children: [
      {
        path: '/cc/cform',
        name: 'CForm',
        component: () => import('@/views/CommonComponents/CForm.vue'),
        meta: {
          title: '表单',
          auths: ['submit']
        }
      },
      {
        path: '/cc/ctable',
        name: 'CTable',
        component: () => import('@/views/CommonComponents/CTable.vue'),
        meta: {
          title: '表格'
        }
      },
      {
        path: '/cc/dialogform',
        name: 'DialogForm',
        component: () => import('@/views/CommonComponents/CDialogForm.vue'),
        meta: {
          title: 'DialogForm'
        }
      },
    ]
  },
  {
    path: '/vp',
    name: 'permissions',
    component: layout,
    redirect: '/vp/view',
    meta: {
      title: '权限控制',
      icon: 'el-icon-user-solid'
    },
    children: [
      {
        path: '/vp/view',
        name: 'ViewPermissions',
        component: () => import('@/views/auths/ViewPermissions.vue'),
        meta: {
          title: '显示权限'
        }
      },
      {
        path: '/vp/edit',
        name: 'EditPermissions',
        component: () => import('@/views/auths/EditPermissions.vue'),
        meta: {
          title: '编辑权限'
        }
      },
    ]
  },
  {
    path: '*',
    redirect: '/404',
    meta: {
      sidebar: false
    }
  }
]
const authRouteUser = [
  {
    path: '/cc',
    name: 'CommonComponents',
    component: layout,
    redirect: '/cc/cform',
    meta: {
      title: '公用组件',
      icon: 'el-icon-s-grid'
    },
    children: [
      {
        path: '/cc/cform',
        name: 'CForm',
        component: () => import('@/views/CommonComponents/CForm.vue'),
        meta: {
          title: '表单'
        }
      },
    ]
  },
]

const delay = () => new Promise((res) => {
  setTimeout(() => { res() })
})

export default (router) => {
  // router.addRoute 不接受一个数组类型得值 只接受对象
  const addR = (routes) => {
    routes.forEach(route => {
      router.addRoute(route)
    })
  }
  
  router.beforeEach(async (to, from, next) => {
    await delay() // 增加延迟 router总是会比pinia先注册 所以加个延迟即可解决
    const user = useUser()
    const project = useProject()
    
    NProgress.start()

    if (to.path === '/login') return next()
    if (!user.userName) { return next('/login') }
    project.modifyTags({ data: to, type: 'add' })
    
    if (!project.isExistRoute && !project.routes?.length) {
      project.isExistRoute = true
      project.loading = true
      setTimeout(() => {
        project.loading = false
        try {
          if (user.userName === 'admin') {
            addR(authRouteAdmin) // 添加权限路由 模拟后台返回
  
            project.routes = [...router.options.routes, ...authRouteAdmin]
          } else {
            addR(authRouteUser) // 添加权限路由 模拟后台返回
  
            project.routes = [...router.options.routes, ...authRouteUser]
          }
  
          next({ ...to, replace: true })
        } catch (error) {
          console.log(error);
          // next('/login')
        }
      }, 1000)
    } else {
      next()
    }
  })

  router.afterEach(() => {
    NProgress.done()
  })
}
